home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / xlib / tdepth.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  7.2 KB  |  308 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <GL/glx.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <X11/keysym.h>
  21.  
  22. static int RGBattributes[] = {
  23.     GLX_RGBA,
  24.     GLX_RED_SIZE, 1,
  25.     GLX_GREEN_SIZE, 1,
  26.     GLX_BLUE_SIZE, 1,
  27.     GLX_DEPTH_SIZE, 1,
  28.     None,
  29. };
  30.  
  31. static int CIattributes[] = {
  32.     GLX_DEPTH_SIZE, 1,
  33.     None,
  34. };
  35.  
  36. enum {
  37.     BLACK = 0,
  38.     RED,
  39.     GREEN,
  40.     YELLOW,
  41.     BLUE,
  42.     MAGENTA,
  43.     CYAN,
  44.     WHITE
  45. };
  46.  
  47. #define COLOR_OFFSET_1    16
  48. #define COLOR_OFFSET_2    32
  49.  
  50.  
  51. static float rgbMap[][3] = {
  52.     {0, 0, 0},
  53.     {1, 0, 0},
  54.     {0, 1, 0},
  55.     {1, 1, 0},
  56.     {0, 0, 1},
  57.     {1, 0, 1},
  58.     {0, 1, 1},
  59.     {1, 1, 1}
  60. };
  61.  
  62. static long W = 300, H = 300;
  63. static long size, mode, rgb;
  64. static long antiAlias = 0;
  65. static long stipple = 0;
  66. static long ci1 = BLUE, ci2 = GREEN;
  67. static unsigned char stippleBits[32*4] = {
  68.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  69.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  70.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  71.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  72.  
  73.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  74.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  75.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  76.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  77.  
  78.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  79.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  80.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  81.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  82.  
  83.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  84.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  85.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  86.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  87. };
  88.  
  89.  
  90. static void DoDisplay(void)
  91. {
  92.     glEnable(GL_SCISSOR_TEST);
  93.     glEnable(GL_DEPTH_TEST);
  94.  
  95.     glViewport(10, 10, W-20, H-20);
  96.     glScissor(10, 10, W-20, H-20);
  97.  
  98.     glMatrixMode(GL_PROJECTION);
  99.     glLoadIdentity();
  100.     glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
  101.     glMatrixMode(GL_MODELVIEW);
  102.  
  103.     glClearColor(0.0, 0.0, 0.0, 0.0);
  104.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  105.  
  106.     if (antiAlias) {
  107.     glEnable(GL_POLYGON_SMOOTH);
  108.     glEnable(GL_BLEND);
  109.     glDisable(GL_DEPTH_TEST);
  110.     glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  111.     } else {
  112.     glDisable(GL_POLYGON_SMOOTH);
  113.     glDisable(GL_BLEND);
  114.     glEnable(GL_DEPTH_TEST);
  115.     }
  116.     if (stipple) {
  117.     glPolygonStipple(stippleBits);
  118.     glEnable(GL_POLYGON_STIPPLE);
  119.     } else {
  120.     glDisable(GL_POLYGON_STIPPLE);
  121.     }
  122.  
  123.     /*
  124.     ** Draw two triangles that intersect each other.
  125.     */
  126.     glBegin(GL_TRIANGLES);
  127.         rgb ? glColor3fv(rgbMap[BLUE]) : glIndexi(ci1);
  128.     glVertex3f( 0.9, -0.9, -30.0);
  129.     glVertex3f( 0.9,  0.9, -30.0);
  130.     glVertex3f(-0.9,  0.0, -30.0);
  131.     rgb ? glColor3fv(rgbMap[GREEN]) : glIndexi(ci2);
  132.     glVertex3f(-0.9, -0.9, -40.0);
  133.     glVertex3f(-0.9,  0.9, -40.0);
  134.     glVertex3f( 0.9,  0.0, -25.0);
  135.     glEnd();
  136.  
  137.     glFlush();
  138. }
  139.  
  140. static void Usage(void)
  141. {
  142.     printf("Usage: tdepth [-c]\n");
  143.     printf("   -c:  Run in color index mode\n");
  144.     exit(-1);
  145. }
  146.  
  147. static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
  148. {
  149.     if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
  150.     return GL_TRUE;
  151.     }
  152.     return GL_FALSE;
  153. }
  154.  
  155. int main(long argc, char** argv)
  156. {
  157.     XVisualInfo *vi;
  158.     Display *dpy;
  159.     Colormap cmap;
  160.     Window window;
  161.     XSetWindowAttributes swa;
  162.     GLXContext cx;
  163.     XEvent event;
  164.     GLboolean needDisplay;
  165.     int i;
  166.  
  167.     rgb = 1;
  168.     for (i = 1; i < argc; i++) {
  169.         if (argv[i][0] == '-') {
  170.             switch (argv[i][1]) {
  171.               case 'c':
  172.                 rgb = 0;
  173.                 break;
  174.               default:
  175.                 Usage();
  176.             }
  177.         } else {
  178.             Usage();
  179.         }
  180.     }
  181.  
  182.     dpy = XOpenDisplay(0);
  183.     if (!dpy) {
  184.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  185.     return -1;
  186.     }
  187.  
  188.     vi = glXChooseVisual(dpy, DefaultScreen(dpy),
  189.              rgb ? RGBattributes : CIattributes);
  190.     if (!vi) {
  191.     fprintf(stderr, "No appropriate visual on \"%s\"\n",
  192.         getenv("DISPLAY"));
  193.     return -1;
  194.     }
  195.  
  196.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  197.                rgb ? AllocNone : AllocAll);
  198.     swa.border_pixel = 0;
  199.     swa.colormap = cmap;
  200.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
  201.     | KeyReleaseMask;
  202.     window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  203.                W, H,
  204.                0, vi->depth, InputOutput, vi->visual,
  205.                CWBorderPixel|CWColormap|CWEventMask, &swa);
  206.     XSetWMColormapWindows(dpy, window, &window, 1);
  207.     XMapWindow(dpy, window);
  208.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
  209.  
  210.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  211.     if (!glXMakeCurrent(dpy, window, cx)) {
  212.     fprintf(stderr, "Can't make window current to context\n");
  213.     return -1;
  214.     }
  215.  
  216.     if (!rgb) {
  217.     XColor xc[32+2];
  218.  
  219.     /* Build two color ramps to use */
  220.     unsigned long pixel;
  221.     unsigned short red, green, blue;
  222.     char flags;  /* do_red, do_green, do_blue */
  223.     char pad;
  224.  
  225.     for (i = 0; i < 16; i++) {
  226.         xc[i].pixel = i + COLOR_OFFSET_1;
  227.         xc[i].red = 0;
  228.         xc[i].green = 0;
  229.         xc[i].blue = (unsigned short) (65535.0 * (i / 15.0));
  230.         xc[i].flags = DoRed | DoGreen | DoBlue;
  231.  
  232.         xc[i+16].pixel = i + COLOR_OFFSET_2;
  233.         xc[i+16].red = 0;
  234.         xc[i+16].green = (unsigned short) (65535.0 * (i / 15.0));
  235.         xc[i+16].blue = 0;
  236.         xc[i+16].flags = DoRed | DoGreen | DoBlue;
  237.     }
  238.  
  239.     xc[32].pixel = BLUE;
  240.     xc[32].red = xc[32].green = 0;
  241.     xc[32].blue = 65535;
  242.     xc[32].flags = DoRed|DoGreen|DoBlue;
  243.  
  244.     xc[32].pixel = GREEN;
  245.     xc[32].red = xc[32].blue = 0;
  246.     xc[32].green = 65535;
  247.     xc[32].flags = DoRed|DoGreen|DoBlue;
  248.     XStoreColors(dpy, cmap, xc, 34);
  249.     }
  250.  
  251.     mode = 0;
  252.     size = 1;
  253.  
  254.     needDisplay = GL_TRUE;
  255.     for (;;) {
  256.     do {
  257.         XNextEvent(dpy, &event);
  258.         switch (event.type) {
  259.           case Expose:
  260.         needDisplay = GL_TRUE;
  261.         break;
  262.           case ConfigureNotify:
  263.         W = event.xconfigure.width;
  264.         H = event.xconfigure.height;
  265.         needDisplay = GL_TRUE;
  266.         break;
  267.           case KeyPress:
  268.         {
  269.             char buf[100];
  270.             int rv;
  271.             KeySym ks;
  272.  
  273.             rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  274.             switch (ks) {
  275.               case XK_A:
  276.               case XK_a:
  277.             antiAlias = !antiAlias;
  278.             if (!rgb) {
  279.                 if (antiAlias) {
  280.                 ci1 = COLOR_OFFSET_1;
  281.                 ci2 = COLOR_OFFSET_2;
  282.                 } else {
  283.                 ci1 = BLUE;
  284.                 ci2 = GREEN;
  285.                 }
  286.             }
  287.             needDisplay = GL_TRUE;
  288.             break;
  289.               case XK_S:
  290.               case XK_s:
  291.             stipple = !stipple;
  292.             needDisplay = GL_TRUE;
  293.             break;
  294.               case XK_Escape:
  295.             return 0;
  296.             }
  297.         }
  298.         break;
  299.         }
  300.     } while (XPending(dpy) != 0);
  301.  
  302.     if (needDisplay) {
  303.         needDisplay = GL_FALSE;
  304.         DoDisplay();
  305.     }
  306.     }
  307. }
  308.